package com.pureut.quality.controller;

//import com.pureut.common.core.utils.poi.ExcelUtil;
import com.pureut.common.core.constant.Constants;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.quality.domain.dto.QualityInspectionDto;
import com.pureut.quality.domain.vo.QualityInspectionVo;
import com.pureut.quality.export.QualityInspectionExport;
import com.pureut.quality.service.IQualityInspectionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.pureut.quality.util.ExcelUtil;


import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description 检验项目控制层
 * @Param
 * @Date 23-02-01
 * @Author hl
 **/
@RestController
@RequestMapping("/inspection")
public class QualityInspectionController extends BaseController {

    @Resource
    private IQualityInspectionService qualityInspectionService;

    /**
     * 查询检验项目
     */
    @RequiresPermissions("quality:verifyconfig:testitem:list")
    @GetMapping("/list")
    public TableDataInfo list(QualityInspectionVo qualityInspectionVo)
    {
        startPage();
        List<QualityInspectionDto> list = qualityInspectionService.selectInspectionList(qualityInspectionVo);
        return getDataTable(list);
    }

    /**
     * 新增检验项目
     */
    @RequiresPermissions("quality:verifyconfig:testitem:add")
    @Log(title = "检验项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody QualityInspectionVo qualityInspectionVo) throws Exception {
        return toAjax(qualityInspectionService.insertInspection(qualityInspectionVo));
    }

    /**
     * 批量删除检验项目
     */
    @RequiresPermissions("quality:verifyconfig:testitem:remove")
    @Log(title = "检验项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemId}")
    public AjaxResult remove(@PathVariable("itemId") String itemId){
        return toAjax(qualityInspectionService.deleteInspectionById(itemId));
    }

    /**
     * 根据编号获取详细信息
     */
    @RequiresPermissions("quality:verifyconfig:testitem:query")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInspectionInfo(@PathVariable long itemId)
    {
        return AjaxResult.success(qualityInspectionService.selectInspectionById(itemId));
    }

    /**
     * 修改检验项目
     */
    @RequiresPermissions("quality:verifyconfig:testitem:edit")
    @Log(title = "检验项目", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody QualityInspectionVo qualityInspectionVo){
        return toAjax(qualityInspectionService.updateInspectionEdit(qualityInspectionVo));
    }

    /**
     * 生成检验项目导入模板
     */
    @PostMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        String name = "检验项目";
        String fileName = Constants.STATIC_URL + "导入" + name + ".xlsx";
        File file = new File(fileName);
        ByteArrayOutputStream baos = null;
        ServletOutputStream out = null;
        try {

            InputStream inStream=new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("导入检验项目.xlsx","UTF-8"));
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.flush();
            baos.close();
            out.flush();
            out.close();
        }
    }

    /**
     * 检验项目信息导入
     */
    @RequiresPermissions("quality:verifyconfig:testitem:import")
    @Log(title = "检验项目导入", businessType = BusinessType.INSERT)
    @PostMapping("/qualityInspectionImport")
    public AjaxResult qualityInspectionImport(MultipartFile file) throws Exception {
        ExcelUtil<QualityInspectionExport> excelUtil = new ExcelUtil<QualityInspectionExport>(QualityInspectionExport.class);
        List<QualityInspectionExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(qualityInspectionService.importMouldData(coCustomer));
    }

}
